package com.ymt.bpm.web.rest.engine.taskcenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PPiHi;
import com.ymt.bpm.service.engine.task.PiService;
import com.ymt.bpm.service.engine.taskcenter.PortalProcessService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/12.
 */
@RestController
@RequestMapping("/api/v1/engine/taskcenter/process")
public class ProcessResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PortalProcessService pps;

    @Autowired
    private PiService pis;

    private int processFavLimit = Platform.getInt("portal.process-fav-limit");

    @RequestMapping(value="bpdShortcuts", method = RequestMethod.GET)
    public JSONObject getBpdShortcuts(HttpServletRequest req) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        int frequentLimits = Platform.getInt("portal.process-frq-limit");
        int favouriteLimits = Platform.getInt("portal.process-fav-limit");
        int templateLimits = Platform.getInt("portal.process-tpl-limit");
        if (log.isDebugEnabled()) {
            log.debug("bpdShortcuts tenantId="+tenantId);
            log.debug("bpdShortcuts loginName="+loginName);
            log.debug("bpdShortcuts frequentLimits="+frequentLimits);
            log.debug("bpdShortcuts favouriteLimits="+favouriteLimits);
            log.debug("bpdShortcuts templateLimits="+templateLimits);
        }
        JSONObject rtn = new JSONObject();
        JSONArray frequent = pps.getFrequentlyBpd(tenantId, loginName, frequentLimits);
        JSONArray favourite = pps.getFavouriteBpd(tenantId, loginName, favouriteLimits);
        JSONArray template = pps.getTemplates(tenantId, loginName, templateLimits);
        rtn.put("frequent", frequent);
        rtn.put("favourite", favourite);
        rtn.put("template", template);
        return rtn;
    }

    @RequestMapping(value="addFav", method = RequestMethod.GET)
    public JSONObject addFav(HttpServletRequest req,
                                      @RequestParam(value="bpdId") String bpdId,
                                      @RequestParam(value="bpdName") String bpdName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        if (log.isDebugEnabled()) {
            log.debug("addFav tenantId="+tenantId);
            log.debug("addFav loginName="+loginName);
            log.debug("addFav bpdId="+bpdId);
            log.debug("addFav bpdName="+bpdName);
        }
        return pps.addFav(tenantId, loginName, bpdId, bpdName, processFavLimit);
    }

    @RequestMapping(value="delFav", method = RequestMethod.DELETE)
    public JSONObject delFav(HttpServletRequest req,
                             @RequestParam(value="bpdId") String bpdId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        if (log.isDebugEnabled()) {
            log.debug("delFav tenantId="+tenantId);
            log.debug("delFav loginName="+loginName);
            log.debug("delFav bpdId="+bpdId);
        }
        return pps.delFav(tenantId, loginName, bpdId);
    }

    @RequestMapping(value="getBpdAuthTree", method = RequestMethod.GET)
    public JSONArray getBpdAuthTree(HttpServletRequest req,
                                    @RequestParam("authType")String authType) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        if (log.isDebugEnabled()) {
            log.debug("getBpdAuthTree tenantId="+tenantId);
            log.debug("getBpdAuthTree loginName="+loginName);
        }
        return pps.getBpdAuthTree(tenantId, loginName, authType);
    }

    @RequestMapping(value="getBpdSvg", method = RequestMethod.GET)
    public JSONObject getBpdSvg(HttpServletRequest req,
                                    @RequestParam("bpdId")String bpdId,
                                @RequestParam("versionid")String versionid) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("getBpdSvg tenantId="+tenantId);
            log.debug("getBpdSvg bpdId="+bpdId);
            log.debug("getBpdSvg versionid="+versionid);
        }
        return pps.getBpdSvg(tenantId, bpdId, versionid);
    }

    @PostMapping(value="hasten")
    public JSONObject hasten(HttpServletRequest req,
                                @RequestParam("piId")String piId,
                                @RequestParam("msg")String msg) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        JSONObject params = new JSONObject();
        params.put("piId", piId);
        params.put("msg", msg);
        if (log.isDebugEnabled()) {
            log.debug("hasten tenantId="+tenantId);
            log.debug("hasten loginName="+loginName);
            log.debug("hasten displayName="+displayName);
            log.debug("hasten params="+params);
        }
        return pps.hasten(tenantId, loginName, displayName, params);
    }

    @GetMapping(value="recall")
    public JSONObject recall(HttpServletRequest req,
                             @RequestParam("piId")String piId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("recall tenantId="+tenantId);
            log.debug("recall loginName="+loginName);
            log.debug("recall displayName="+displayName);
            log.debug("recall piId="+piId);
        }
        return pps.recall(tenantId, loginName, displayName, piId);
    }

    @GetMapping(value="revoke")
    public JSONObject revoke(HttpServletRequest req,
                             @RequestParam("piId")String piId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("revoke tenantId="+tenantId);
            log.debug("revoke loginName="+loginName);
            log.debug("revoke displayName="+displayName);
            log.debug("revoke piId="+piId);
        }
        return pis.statePiToHi(tenantId, loginName, displayName,
                piId, PPiHi.STATUS_TERMINATED, "REVOKE");
    }

}
